package com.example.softwareconfigurationcoursedesign;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    static final String JDBC_URL = "jdbc:mysql://localhost:3306/course_design";
    static final String DB_USER = "root";
    static final String DB_PASSWORD = "123456";

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        System.err.println("login do get");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String userPhoneNum = request.getParameter("phoneNumber");
        String R_code = request.getParameter("generatedCode2");
        String code = request.getParameter("VerificationCode1");
        System.err.println("register");
        System.err.println(userPhoneNum);
        System.err.println(username);
        System.err.println(password);
        System.err.println(R_code);
        System.err.println(code);
        if (code.equals(R_code)) {
            // 数据库连接和插入操作
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                Connection connection = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PASSWORD);
                String insertQuery = "INSERT INTO users(phone_number,name,password) VALUES (?,?,?)";
                PreparedStatement preparedStatement = connection.prepareStatement(insertQuery);
                preparedStatement.setString(1, userPhoneNum);
                preparedStatement.setString(2, username);
                preparedStatement.setString(3, password);
                preparedStatement.executeUpdate();
                preparedStatement.close();
                connection.close();
//            response.sendRedirect("Register.html");
//            response.sendRedirect("TransitionInterface.html");
                PrintWriter out = response.getWriter();
                out.println("<script> alert('注册成功 将返回主页面 请您进行登录'); window.location.href='index.jsp';</script>");
                out.flush();
                System.out.println("注册用户");
            } catch (SQLException e) {
                e.printStackTrace(System.err);
                // 处理数据库错误
                response.sendRedirect("error.html"); // 或者返回适当的错误页面
            } catch (ClassNotFoundException e) {
                System.err.println("dabsleiufble");
                e.printStackTrace(System.err);
            }
        } else {
            PrintWriter out = response.getWriter();
            out.println("<script> alert('注册失败 验证码输入错误 请您确认后再次输入'); window.location.href='index.jsp';</script>");
            out.flush();
        }
    }
}